﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using TextEditor.TextDocument.Document;
using System.Drawing;
using ZYCommon;
using Bifrost;

namespace TextEditor.TextDocument
{
    public class DocPrint
    {
        PrintDocument pd;
        ZYTextDocument document;
        public DocPrint(ZYTextDocument _document)
        {
            pd = new PrintDocument();
            document = _document;
            PaperSet();
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
        }
        int i= 0;
        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                int margin = 0;
                Graphics innerGraph = document.View.GetInnerGraph();
                PrintPage page = document.Pages[i];
                document.View.Graph = e.Graphics;
                document.Pages.DrawPageFrame(-e.MarginBounds.Left - margin, -e.MarginBounds.Top, e.Graphics, false, Color.White, i + 1, this.document);
                Rectangle rect = new Rectangle(page.Left, page.Top, page.Width + 10, page.Height + (i == document.Pages.Count - 1 ? 2 : 0));
                document.View.TranslateTransform(0 - margin, document.Pages.TopMargin - page.Top - e.MarginBounds.Top);
                e.Graphics.SetClip(rect);
                bool showParagraphFlag = document.Info.ShowParagraphFlag;
                document.Info.ShowParagraphFlag = false;
                document.Info.FieldUnderLine = false;
                document.Info.ShowExpendHandle = false;
                document.ViewPaint(e.Graphics, rect);
                document.View.Graph = innerGraph;
                document.Info.ShowParagraphFlag = showParagraphFlag;
                i++;
                if (i < this.document.Pages.Count)
                {
                    e.HasMorePages = true;
                }
                else
                {
                    e.HasMorePages = false;
                }
            }
            catch
            {
                App.Msg("打印异常！");
            }
        }

        private void PaperSet()
        {
            PaperSizeSet();
            pd.DefaultPageSettings.Margins.Left = this.document.Pages.LeftMargin;
            pd.DefaultPageSettings.Margins.Right = this.document.Pages.RightMargin;
            pd.DefaultPageSettings.Margins.Top = this.document.Pages.TopMargin;
            pd.DefaultPageSettings.Margins.Bottom = this.document.Pages.BottomMargin;
            pd.OriginAtMargins = true;
        }

        private void PaperSizeSet()
        {
            foreach (PaperSize _papersize in pd.PrinterSettings.PaperSizes)
            {
                if (this.document.Pages.PaperKindName == _papersize.PaperName)
                {
                    //_papersize.Kind = PaperKind.Custom;
                    pd.DefaultPageSettings.PaperSize = _papersize;
                    return;
                }
            }

            pd.DefaultPageSettings.PaperSize = new PaperSize("custom16", this.document.Pages.PaperWidth, this.document.Pages.PaperHeight);
            //if (myPages.paperSizeName == "custom16")
            //{
            //    string defaultPrinter = this.pdDocument.PrinterSettings.PrinterName;
            //    MJMCustomPrintForm.MJMCustomPrintForm.AddCustomPaperSize(defaultPrinter, "custom16", 190f, 260f);
            //    MJMCustomPrintForm.MJMCustomPrintForm.AddMjm104MmPaperSizeToDefaultPrinter();
            //    foreach (PaperSize _papersize in this.pdDocument.PrinterSettings.PaperSizes)
            //    {
            //        if (this.myPages.paperSizeName == _papersize.PaperName)
            //        {
            //            pdDocument.DefaultPageSettings.PaperSize = _papersize;
            //        }
            //    }
            //}
        }

        public void print()
        {
            this.pd.Print();
        }
    }
}
